import { defineStore } from "pinia";
import {
  quantityStatistics,
  batchStatistics,
} from "../../utils/api/statistics/statistics";
// import { useMemberStore } from "@/stores/modules/member";
const quantityTitle = [
  { text: "物资名称", style: "width:150px;", name: "GoodsName" },
  { text: "物资类型", style: "width:150px;", name: "GoodsTypeName" },
  { text: "库存数量/单位", style: "width:150px;", name: "GoodsNumber" },
];

const batchTitle = [
  { text: "物资名称", style: "width:150px;", name: "GoodsName" },
  { text: "物资类型", style: "width:64px;", name: "GoodsTypeName" },
  { text: "物资批次", style: "width:100px;", name: "GoodsBatch" },
  { text: "有效期", style: "width:96px;", name: "ExpirationDate" },
  { text: "库位", style: "width:164px;", name: "BoxName" },
  { text: "库存数量/单位", style: "width:150px;", name: "GoodsNumber" },
];

export const useStatisticStore = defineStore(
  "statisticsPage",
  () => {
    // const useMember = useMemberStore()
    let select = ref("quantity"); // 选中状态 'quantity'为数量统计,'batch'为批次统计

    const titleStatus = computed(() => {
      switch (select.value) {
        case "quantity":
          return quantityTitle;

        case "batch":
          return batchTitle;

        default:
          break;
      }
    }); // 表格标题

    const dataList = ref([]); // 列表数据
    let total = ref(0); // 传入分页器的数据总条数

    const getStatisticsData = async (e: object) => {
      switch (select.value) {
        case "quantity":
          let resQuantity = await quantityStatistics(e);
          dataList.value = resQuantity.data.list;
          total.value = resQuantity.data.totalCount;
          break;

        case "batch":
          let resBatch = await batchStatistics(e);
          dataList.value = resBatch.data.list;
          total.value = resBatch.data.totalCount;
          break;
        default:
          break;
      }
    };

    return { select, titleStatus, dataList, total, getStatisticsData };
  },
  // TODO: 持久化
  {
    // 配置持久化
    persist: {
      // 调整为兼容多端的API
      storage: {
        setItem(key, value) {
          uni.setStorageSync(key, value);
        },
        getItem(key) {
          return uni.getStorageSync(key);
        },
      },
      paths: [],
    },
  }
);
